package com.lagou.comment.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.lagou.comment.CommentService;
import com.lagou.entity.CourseComment;
import mapper.CourseCommentDao;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service  // 暴露服务
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CourseCommentDao courseCommentDao;


    // 保存评论
    @Override
    public Integer saveComment(CourseComment comment) {
        return courseCommentDao.saveComment(comment);
    }

    // 通过id 获取评论列表
    @Override
    public List<CourseComment> getCommentsByCourseId(Integer courseid, Integer offset, Integer pageSize) {
        return courseCommentDao.getCommentsByCourseId(courseid,offset, pageSize);
    }

    // 保存点赞信息
    @Override
    public Integer saveFavorite(Integer comment_id, Integer userid) {
        Integer i = courseCommentDao.existsFavorite(comment_id, userid);
        int i1 = 0;
        int i2 = 0;
        if(i == 0){ //没点过赞
            i1 = courseCommentDao.saveCommentFavorite(comment_id,userid);
        }else{
            i1 = courseCommentDao.updateFavoriteStatus(0,comment_id,userid);
        }
        // 更新点赞数量
        i2 = courseCommentDao.updateLikeCount(1,comment_id);

        if(i1==0 || i2==0){
            throw  new RuntimeException("点赞失败！");
        }
        return comment_id;
    }

    // 取消点赞信息
    @Override
    public Integer cancelFavorite(Integer comment_id, Integer userid) {
        Integer i1 = courseCommentDao.updateFavoriteStatus(1, comment_id, userid);
        Integer i2 = courseCommentDao.updateLikeCount(-1,comment_id);

        if(i1==0 || i2==0){
            throw  new RuntimeException("取消赞失败！");
        }
        return i2;
    }
}
